package com.ygj.announcement.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.base.common.persistent.dao.DictnameDAO;
import com.base.common.persistent.dao.NotesDAO;
import com.base.common.persistent.model.Employee;
import com.base.common.utils.PinyinConv;
import com.base.common.utils.Similarity;
import com.gexin.rp.sdk.base.uitls.Base64Util;
import com.ygj.announcement.service.AnnouncementService;
import com.ygj.base.common.persistent.model.Dictname;
import com.ygj.base.common.persistent.model.Notes;
import com.ygj.base.webservice.entity.announcement.Note;

@Service("announcementService")
public class AnnouncementServiceImpl implements AnnouncementService {

	@Resource
	private NotesDAO notesDAO;
	
	@Resource
	private DictnameDAO dictnameDAO;

	@Override
	public List<Notes> queryAll(int page) {
		List<Notes> notes = new ArrayList<Notes>();
		notes = notesDAO.findBySqlAndPaging((page - 1)*15 + 1, page * 15);
//		System.out.println("----------------------------------------------");
// "select model.fdate as fdate, model.ftitle as ftitle, model.fbm as fbm, model.rowguid as rowguid from Notes as model where model.fpub = '1' and model.fstate='批准' order by model.fdate desc"
		return notes;
	}

	@Override
	public Note queryDetailedInformationOfAnnouncementByRowguid(String rowguid) {
		Notes notes = notesDAO.findByProperty(rowguid).get(0);
		Note note = new Note();
		note.setFbm(notes.getFbm());
		note.setFclass(notes.getFclass());
		note.setFcom(notes.getFcom());
		note.setFcon(notes.getFcon());
		note.setFcurmen(notes.getFcurmen());
		note.setFdate(notes.getFdate());
		note.setFfb(notes.getFfb());
		note.setFmen1(notes.getFmen1());
		note.setFmen2(notes.getFmen2());
		note.setFmt(notes.getFmt());
		note.setFmj(notes.getFmj());
		note.setFno(notes.getFno());
		note.setFoc(notes.getFoc());
		note.setFps(notes.getFps());
		note.setFpub(notes.getFpub());
		note.setFpubtime(notes.getFpubtime());
		note.setFseed(notes.getFseed());
		note.setFsend(notes.getFsend());
		note.setFser(notes.getFser());
		note.setFsize(notes.getFsize());
		note.setFsp(notes.getFsp());
		note.setFstate(notes.getFstate());
		note.setFtitle(notes.getFtitle());
		note.setFupdate(notes.getFupdate());
		note.setRowguid(notes.getRowguid());
		note.setFstmStr(Base64Util.getBASE64(notes.getFstm()));
		return note;
	}

	@Override
	public int queryAllTotalCounts() {
		int page = notesDAO.findTotalPagesByProperty();
		return page;
	}

	/* (non-Javadoc)
	 * @see com.ygj.announcement.service.AnnouncementService#queryOaPeople()
	 */
	@Override
	public List queryOaPeople(String name) {
		List<Employee> employees = new ArrayList<Employee>();
		
		List<Dictname> dictnames = dictnameDAO.queryOaPeople();
		
		for(Dictname dictname : dictnames){
			if(dictname != null){
				Employee employee = new Employee();
				employee.setName(dictname.getFname());
				employees.add(employee);
			}
		}
		
		List<Double> list = new ArrayList<Double>();
		// name相似度匹配和排序
		for(int i = 0; i < employees.size(); i++){
			String fnamePinyin = PinyinConv.cn2py(employees.get(i).getName());
			name = PinyinConv.cn2py(name).toLowerCase();
			String strA = "";
			String strB = "";
			if(name.length() >= fnamePinyin.length()){
				strA = name;
				strB = fnamePinyin;
			} else {
				strA = fnamePinyin;
				strB = name;
			}
			double similarDegree = Similarity.SimilarDegree(strA, strB);
			list.add(similarDegree);
		}
		
		for(int i = 0; i < list.size(); i++) {
			for(int j = i; j < list.size(); j++){
				Employee employee = employees.get(i);
				double a = list.get(i);
				double b = list.get(j);
				if(a < b){
					list.set(i, b);
					list.set(j, a);
					employees.set(i, employees.get(j));
					employees.set(j, employee);
				}
			}
		}
		
		for(int k = 0; k < list.size(); k++){
			if(list.get(k) == 0){
				employees.remove(k);
				list.remove(k);
			}
		}
		
		return employees;
	}
	
	
	
}
